﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
  public partial class SdreoMasterList : InvestorAdminPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      listFilter.SearchButton.Click += new ImageClickEventHandler(SearchButton_Click);
      RadAjaxManager.AjaxSettings.AddAjaxSetting(Grid, Grid);
    }

    void SearchButton_Click(object sender, ImageClickEventArgs e)
    {
      Grid.Rebind();
      listFilter.ClearFilter();
    }

    protected override string GetPropertyDetailsColumnEditorID()
    {
      return edTextBox.ID;
    }

    protected override Control GetGridContainer()
    {
      return phGrid;
    }

    protected override string GetDataSourceID()
    {
      return sqlDataSource.ID;
    }

    protected void sqlDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
    }

    protected void lnkExportToExcel_Click(object sender, EventArgs e)
    {
      HideColumn("DeleteButton");
      HideColumn("EditButton");
      //HideColumn("Address");

      Grid.MasterTableView.ExportToExcel();
    }

    protected void lnkRefreshGrid_Click(object sender, EventArgs e)
    {
      Grid.Rebind();
    }

    protected void lnkClearList_Click(object sender, EventArgs e)
    {
      //DataAccess da = new DataAccess();
      //if (da.ExecuteSQLRowCount("clearSdreo_MasterList") > 0)
      //{
      //  CountsControl.SetMessage("SDREO List deleted.");
      //  Grid.Rebind();
      //}
      //else { CountsControl.SetMessage("No properties to delete."); }
    }

    protected void lnkClearImages_Click(object sender, EventArgs e)
    {
      DataAccess da = new DataAccess();

      //Delete Images
      string[] fileImagePaths = Directory.GetFiles(Server.MapPath("~/DocSecure/SDREO/PropertyImages/"));
      foreach (string filePath in fileImagePaths)
        File.Delete(filePath);

      //Delete documents
      string[] fileDocPaths = Directory.GetFiles(Server.MapPath("~/DocSecure/SDREO/PropertyDocuments/"));
      foreach (string filePath in fileDocPaths)
        File.Delete(filePath);

      da.ExecuteSQLNonQuery("clearSdreo_PropertyDocsAndImages");

      //CountsControl.SetMessage("All Docs and Images have been deleted.");
    }

    protected override SqlDataSource GetSqlDataSource()
    {
      return sqlDataSource;
    }

    protected override ListFilterControl GetListFilterControl()
    {
      return listFilter;
    }

    protected override bool IsEditable()
    {
      return
        CurrentUser.UserRole == UserRole.SuperAdmin
        || (CurrentUser.UserRole == UserRole.User
            && CurrentUser.SdreoRole == RoleAccess.ReadWrite)
        || (CurrentUser.UserRole == UserRole.InvestorAdmin
            && CurrentUser.SubRole == UserSubRole.SDReo);
    }

    protected override SourcePage GetSourcePage()
    {
      return SourcePage.SdreoList;
    }

    protected override void SetupSqlDataSource(SqlDataSource dataSource)
    {
      base.SetupSqlDataSource(dataSource);

      dataSource.Selecting += new SqlDataSourceSelectingEventHandler(dataSource_Selecting);
    }

    void dataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
      e.Command.Parameters["@showAllUnassigned"].Value = showAllUnassigned;
    }

    bool showAllUnassigned = false;

    protected void lnkShowUnassigned_Click(object sender, EventArgs e)
    {
      showAllUnassigned = true;
      Grid.Rebind();
    }
  }
}
